# -*- mode: python; -*-
#
# Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
#


import platform
Import('AgentEnv')
env = AgentEnv.Clone()

# Generate the sandesh UVE source files
vrouter_pkg = env.SandeshGenPy('vrouter.sandesh', 'vrouter/', False)
loadbalancer_pkg = env.SandeshGenPy('loadbalancer.sandesh', 'vrouter/', False)
virtual_machine_pkg = env.SandeshGenPy('virtual_machine.sandesh', 'vrouter/sandesh/', False)
virtual_network_pkg = env.SandeshGenPy('virtual_network.sandesh', 'vrouter/sandesh/', False)
cpuinfo_pkg = env.SandeshGenPy('#src/contrail-common/base/sandesh/cpuinfo.sandesh', 'vrouter/vrouter/', False)
process_info_pkg = env.SandeshGenPy('#src/contrail-common/base/sandesh/process_info.sandesh', 'vrouter/vrouter/', False)
ds_results_pkg = env.SandeshGenPy('#src/contrail-common/sandesh/library/common/derived_stats_results.sandesh', 'vrouter/vrouter/', False)
virtual_machine_port_bmap_pkg = env.SandeshGenPy('port_bmap.sandesh', 'vrouter/sandesh/virtual_machine/', False)
flow_pkg = env.SandeshGenPy('#controller/src/sandesh/common/flow.sandesh', 'vrouter/sandesh/', False)
interface_pkg = env.SandeshGenPy('interface.sandesh', 'vrouter/sandesh/', False)
interface_port_bmap_pkg = env.SandeshGenPy('port_bmap.sandesh', 'vrouter/sandesh/interface/', False)
env.Depends(cpuinfo_pkg, vrouter_pkg)
env.Depends(process_info_pkg, vrouter_pkg)
env.Depends(ds_results_pkg, vrouter_pkg)
env.Depends(virtual_machine_port_bmap_pkg, virtual_machine_pkg)
env.Depends(interface_port_bmap_pkg, interface_pkg)

setup_sources = [
           'setup.py',
           'MANIFEST.in',
          ]

setup_sources_rules = []
for file in setup_sources:
    setup_sources_rules.append(env.Install("", "#controller/src/vnsw/agent/uve/" + file))

vrouter_sources = [
        'cpuinfo.py',
        'mock_generator.py',
       ]

vrouter_sources_rules = []
for file in vrouter_sources:
    vrouter_sources_rules.append(env.Install('vrouter', '#controller/src/vnsw/agent/uve/' + file))

sandesh_init_rules = []
sandesh_init_rules.append(env.Install('vrouter', env['TOP'] + '/vnsw/agent/uve/vrouter/vrouter/__init__.py'))
sandesh_init_rules.append(env.Install('vrouter/sandesh', env['TOP'] + '/vnsw/agent/uve/vrouter/vrouter/__init__.py'))
env.Depends(sandesh_init_rules, vrouter_pkg)

rel_path = Dir('vrouter').path
def BuildInfoAction(target, source, env):
    env.GenerateBuildInfoPyCode(path=rel_path)
    
build_info_rules = [env.Command(target='buildinfo.py', source = None, action=BuildInfoAction)]

sdist_depends = []
sdist_depends.extend(setup_sources_rules)
sdist_depends.extend(vrouter_sources_rules)
sdist_depends.extend(vrouter_pkg)
sdist_depends.extend(cpuinfo_pkg)
sdist_depends.extend(process_info_pkg)
sdist_depends.extend(ds_results_pkg)
sdist_depends.extend(virtual_machine_pkg)
sdist_depends.extend(virtual_network_pkg)
sdist_depends.extend(virtual_machine_port_bmap_pkg)
sdist_depends.extend(sandesh_init_rules)
sdist_depends.extend(build_info_rules)
sdist_depends.extend(flow_pkg)
sdist_depends.extend(loadbalancer_pkg)
sdist_depends.extend(interface_pkg)
sdist_depends.extend(interface_port_bmap_pkg)

cd_cmd = 'cd ' + Dir('.').path + ' && '
sdist_gen = env.Command('dist', 'setup.py', cd_cmd + 'python setup.py sdist')

env.Depends(sdist_gen, sdist_depends)
env.Default(sdist_gen)
env.Alias("vrouter:node_mgr", sdist_gen)
env.Alias("nodemgr-full", sdist_gen)

# Generate the C++ source files
SandeshGenFiles  = env.SandeshGenCpp('interface.sandesh')
SandeshGenFiles += env.SandeshGenCpp('port_bmap.sandesh')
SandeshGenFiles += env.SandeshGenCpp('vrouter.sandesh')
SandeshGenFiles += env.SandeshGenCpp('virtual_machine.sandesh')
SandeshGenFiles += env.SandeshGenCpp('virtual_network.sandesh')
SandeshGenFiles += env.SandeshGenCpp('prouter.sandesh')

SandeshGenSrcs = env.ExtractCpp(SandeshGenFiles)
env.Depends(SandeshGenSrcs,'#/build/lib/' + env['LIBPREFIX'] + 'sandesh' + env['LIBSUFFIX'])
SandeshGenObjs = env.Object(SandeshGenSrcs)

StatsSandeshGenFiles = env.SandeshGenCpp('stats_interval.sandesh')
StatsSandeshGenFiles += env.SandeshGenCpp('uve.sandesh')
StatsSandeshGenSrcs = env.ExtractCpp(StatsSandeshGenFiles)
StatsSandeshGenObjs = env.Object(StatsSandeshGenSrcs)

libuve = env.Library('uve',
                     SandeshGenObjs +
                     [
                     'agent_uve_base.cc',
                     'agent_uve.cc',
                     'interface_uve_table.cc',
                     'l4_port_bitmap.cc',
                     'prouter_uve_table.cc',
                     'vm_uve_entry_base.cc',
                     'vm_uve_table_base.cc',
                     'vn_uve_entry_base.cc',
                     'vn_uve_table_base.cc',
                     'vrouter_stats_collector.cc',
                     'vrouter_uve_entry_base.cc'
                     ])

if platform.system() == 'Windows':
    libstatsuve_os_dependent_src = ['windows/vm_stat_stub.cc']
else:
    libstatsuve_os_dependent_src = ['vm_stat.cc', 'vm_stat_kvm.cc', 'vm_stat_docker.cc']

libstatsuve = env.Library('statsuve',
                          StatsSandeshGenObjs +
                         [
                          'agent_uve_stats.cc',
                          'interface_uve_stats_table.cc',
                          'stats_manager.cc',
                          libstatsuve_os_dependent_src,
                          'vm_uve_entry.cc',
                          'vm_uve_table.cc',
                          'vn_uve_entry.cc',
                          'vn_uve_table.cc',
                          'vrouter_uve_entry.cc',
                         ])

Import('BuildEnv')
mockEnv = BuildEnv.Clone()

def MapBuildDir(list):
    return map(lambda x: Dir('../../../' + x).abspath, list)

mockEnv.Prepend(LIBS=[
    'ksync',
    'sandeshflow',
    'sandeshvns',
    'sandesh',
    'http',
    'http_parser',
    'process_info',
    'io',
    'sandesh',
    'base',
    'boost_program_options',
    'curl',
    'pugixml',
     'rt',
    'ssl',
    'crypto'
    ])

if platform.system() != 'Darwin':
    mockEnv.Append(LIBS = ['rt'])

libpath = MapBuildDir(
    [
     'base',
     'io',
     'ksync',
    ])
mockEnv.Append(LIBPATH=libpath)
mockEnv.Prepend(CPPPATH = mockEnv['TOP'])
mockgen = mockEnv.Program('mock_generator',
                          ['mock_generator.cc'])
mockEnv.Alias("vrouter:mock_gen", mockgen)

env.SConscript('test/SConscript', exports='AgentEnv', duplicate=0)
